package com.wtw.flume.utils;

import org.apache.commons.lang.math.NumberUtils;

public class LogUtils {
    /**
     * 校验日志是否为启动日志
     * 校验规则为查看log是否'{' 开头 并且'}'结尾
     */
    public static boolean checkStart(String log) {
//        {"action":"1","ar":"MX","ba":"HTC","detail":"542","en":"start","entry":"2","extend1
//            ":"","g":"S3HQ7LKM@gmail.com","hw":"640*960","l":"en","la":"-43.4","ln":"-98.3","lo
//            ading_time":"10","md":"HTC-5","mid":"993","nw":"WIFI","open_ad_type":"1","os":"8.2.
//            1","sr":"D","sv":"V2.9.0","t":"1559551922019","uid":"993","vc":"0","vn":"1.1.5"}

        if(log == null)
            return false;

        log = log.trim();
        if(log.startsWith("{") && log.endsWith("}"))
            return true;

        return false;
    }

    /**
     * 校验日志是否为事件日志
     * 校验规则为首先查看是否两部分齐全
     * 再分别校对时间戳部分和日志部分
     */
    public static boolean checkEvent(String log) {
        // 服务器时间 | json
        // 1549696569054|{"cm":{"ln":"-89.2","sv":"V2.0.4","os":"8.2.0","g":"M67B4QYU@gmail.com","nw":"4G","
        //l":"en","vc":"18","hw":"1080*1920","ar":"MX","uid":"u8678","t":"1549679122062","la":
        //"-27.4","md":"sumsung-12","vn":"1.1.3","ba":"Sumsung","sr":"Y"},"ap":"weather","et":
        //[]}

        //分割字符串
        String[] split = log.split("\\|");

        if(split.length != 2)
            return false;

        //检验时间戳
        if(split[0].length() != 13 || !NumberUtils.isNumber(split[0]))
            return false;

        //检验json
        if(!split[1].trim().startsWith("{") || !split[1].endsWith("}"))
            return false;

        return true;
    }
}
